package com.crois.barrier.common.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @author Hou Ze Yu
 * @description Door 客户端实体
 * @date 2020/7/8
 */
@AllArgsConstructor
@NoArgsConstructor
@Data
public class BarrierClientInstance implements Serializable {

    private static final double MAX_WEIGHT_VALUE = 10000.0D;
    private static final double MIN_POSITIVE_WEIGHT_VALUE = 0.01D;
    private static final double MIN_WEIGHT_VALUE = 0.00D;

    private static final long serialVersionUID = 2405172041950251807L;
    /***
     * last beat
     */
    private volatile long lastBeat = System.currentTimeMillis();

    /**
     * unique id of this instance.
     */
    private String instanceId;

    /**
     * instance ip
     */
    private String ip;

    /**
     * instance port
     */
    private int port;

    /**
     * instance weight
     */
    private double weight = 1.0D;

    /**
     * instance health status
     */
    private boolean healthy = true;

    /**
     * If instance is enabled to accept request
     */
    private boolean enabled = true;

    /**
     * If instance is ephemeral
     *
     * @since 1.0.0
     */
    private boolean ephemeral = true;

    /**
     * cluster information of instance
     */
    private String clusterName;

    /**
     * Service information of instance
     */
    private String serviceName;

    public String generateInstanceId() {
        return getIp() + "#" + getPort()  + "#" + getServiceName();
    }

    @Override
    public int hashCode() {
        return instanceId.hashCode();
    }

    @Override
    public boolean equals(Object obj) {//this指正在试图存入的对象，obj指HashSet中保存的数据
        if(obj instanceof BarrierClientInstance) {
            BarrierClientInstance barrierClientInstance = (BarrierClientInstance)obj;
            return this.instanceId.equals(barrierClientInstance.instanceId);
        }
        return false;
    }
}
